﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KZ400B
{
    public enum CommEncoding
    { 
        None = 0,
        ASCII = 1
    }
    public enum CommFunctionCode
    {
        Contact               = 0x00,/** 联络指令                 **/ //(@)
        ParameterSetting      = 0x01,/** 基本运行参数设置指令     **/ //($)
        AccelerationSetting   = 0x02,/** 加速度参数设置指令       **/ //(!)
        Run                   = 0x03,/** 运行指令                 **/ //(#)
        Stop                  = 0x04,/** 停止指令                 **/ //(&)
        ToZero                = 0x05,/** 归零指令                 **/ //(%)
        Query                 = 0x06,/** 查询当前位置指令         **/ //(?)
        RelativeZero          = 0x07,/** 相对零位                 **/ //(+)
        StepperAngleFraction  = 0x08,/** 细分参数设置             **/ //(^)
        SingleEnd             = 0x17,/**                          **/ //(;)
        AllEnd                = 0x18,/**                          **/ //(>)
        Response              = 0x19,/**  回答                    **/ //(=)
        Separate              = 0x110,/** 分隔符                  **/ //(,)
        ParameterPreserve     = 0x20, /** 参数保留                **/ //(<)
        VZero                 = 0x21, /** 虚拟零位                **/ //(-)

        Command               = 0x1f0,/**命令**/
        WrongFormat           = 0x1f1,/**命令格式错误**/
        Space                 = 0x200,
        Empty                 = 0x201,
    }

    public class FunctionCode
    {
        public FunctionCode(FunctionCode funCode)
        {
            m_FCode = funCode.m_FCode;
        }

        public FunctionCode(int funCode)
        {
            m_FCode = (CommFunctionCode)funCode;
        }

        public FunctionCode(CommFunctionCode funCode)
        {
            m_FCode = funCode;
        }
        private CommFunctionCode m_FCode = CommFunctionCode.Empty;

        public CommFunctionCode FCode
        {
            get { return m_FCode; }
        }
        public const string Contact              = "T";/** 联络指令**/ //(@)
        public const string ParameterSetting     = "D";/** 基本运行参数设置指令**/ //($)
        public const string AccelerationSetting  = "A";/** 加速度参数设置指令**/ //(!)
        public const string Run                  = "G";/** 运行指令**/ //(#)
        public const string Stop                 = "P";/** 停止指令**/ //(&)
        public const string ToZero               = "H";/** 归零指令**/ //(%)
        public const string Query                = "";/** 查询当前位置指令**/ //(?)
        public const string QuerySuccess         = "?";
        public const string RelativeZero         = "R";
        public const string StepperAngleFraction = "S";/** 细分参数设置             **/ //(^)
        public const string ParameterPreserve    = "C";/** 参数保留                 **/ //(<)
        public const string VZero                = "K";/** 虚拟零位                 **/ //(-)
        public const string SingleEnd            = ""; 
        public const string AllEnd               = "";
        public const string Response             = "";/** 回答**/ //(=)
        public const string Empty                = "";
        public const string Command              = "CMD"; 
        public const string WrongFormat          = "CMD CE"; 

        

        public override string ToString()
        {
            switch (m_FCode)
            {
                case CommFunctionCode.Contact:               return "T";/** 联络指令                 **/ //(@)
                case CommFunctionCode.ParameterSetting:      return "D";/** 基本运行参数设置指令     **/ //($)
                case CommFunctionCode.AccelerationSetting:   return "A";/** 加速度参数设置指令       **/ //(!)
                case CommFunctionCode.Run:                   return "G";/** 运行指令                 **/ //(#)
                case CommFunctionCode.Stop:                  return "P";/** 停止指令                 **/ //(&)
                case CommFunctionCode.ToZero:                return "H";/** 归零指令                 **/ //(%)
                case CommFunctionCode.Query:                 return string.Empty;/** 查询当前位置指令         **/ //(?)
                case CommFunctionCode.RelativeZero:          return "R";/** 相对零位                 **/
                case CommFunctionCode.StepperAngleFraction:  return "S";/** 细分参数设置             **/ //(^)
                case CommFunctionCode.ParameterPreserve:     return "C";/** 参数保留                 **/ //(<)
                case CommFunctionCode.VZero:                 return "K";/** 虚拟零位                 **/ //(-)
                case CommFunctionCode.SingleEnd:             return string.Empty;/**                          **/ //(;)
                case CommFunctionCode.AllEnd:                return string.Empty;/**                          **/ //(>)
                case CommFunctionCode.Response:              return string.Empty;/** 回答                     **/ //(=)
                case CommFunctionCode.Empty:                 return string.Empty;
                case CommFunctionCode.Command:               return Command;
                case CommFunctionCode.WrongFormat:           return "CMD CE";
                default: return string.Empty;
            }
        }

        private static FunctionCode s_fObjContact = new FunctionCode(CommFunctionCode.Contact);
        public static FunctionCode FCodeContact { get { return s_fObjContact; } }
        private static FunctionCode s_fObjParameterSetting = new FunctionCode(CommFunctionCode.ParameterSetting);
        public static FunctionCode FCodeParameterSetting { get { return s_fObjParameterSetting; } }

        private static FunctionCode s_fObjAccelerationSetting = new FunctionCode(CommFunctionCode.AccelerationSetting);
        public static FunctionCode FCodeAccelerationSetting { get { return s_fObjAccelerationSetting; } }
        private static FunctionCode s_fObjRun = new FunctionCode(CommFunctionCode.Run);
        public static FunctionCode FCodeRun { get { return s_fObjRun; } }

        private static FunctionCode s_fObjStop = new FunctionCode(CommFunctionCode.Stop);
        public static FunctionCode FCodeStop { get { return s_fObjStop; } }
        private static FunctionCode s_fObjToZero = new FunctionCode(CommFunctionCode.ToZero);
        public static FunctionCode FCodeToZero { get { return s_fObjToZero; } }

        private static FunctionCode s_fObjQuery = new FunctionCode(CommFunctionCode.Query);
        public static FunctionCode FCodeQuery { get { return s_fObjQuery; } }
        private static FunctionCode s_fObjRelativeZero = new FunctionCode(CommFunctionCode.RelativeZero);
        public static FunctionCode FCodeRelativeZero { get { return s_fObjRelativeZero; } }

        private static FunctionCode s_fObjStepperAngleFraction = new FunctionCode(CommFunctionCode.StepperAngleFraction);
        public static FunctionCode FCodeStepperAngleFraction { get { return s_fObjStepperAngleFraction; } }

        private static FunctionCode s_fObjSingleEnd = new FunctionCode(CommFunctionCode.SingleEnd);
        public static FunctionCode FCodeSingleEnd { get { return s_fObjSingleEnd; } }

        private static FunctionCode s_fObjAllEnd = new FunctionCode(CommFunctionCode.AllEnd);
        public static FunctionCode FCodeAllEnd { get { return s_fObjAllEnd; } }

        private static FunctionCode s_fObjResponse = new FunctionCode(CommFunctionCode.Response);
        public static FunctionCode FCodeResponse { get { return s_fObjResponse; } }

        private static FunctionCode s_fObjWrongFormat = new FunctionCode(CommFunctionCode.WrongFormat);
        public static FunctionCode FCodeWrongFormat { get { return s_fObjWrongFormat; } }


        private static FunctionCode s_fObjEmpty = new FunctionCode(CommFunctionCode.Empty);
        public static FunctionCode FCodeEmpty { get { return s_fObjEmpty; } }

        private static FunctionCode s_fObjParameterPreserve = new FunctionCode(CommFunctionCode.ParameterPreserve);
        public static FunctionCode FCodeParameterPreserve { get { return s_fObjParameterPreserve; } }

        private static FunctionCode s_fObjVZero = new FunctionCode(CommFunctionCode.VZero);
        public static FunctionCode FCodeVZero { get { return s_fObjVZero; } }
    }

    public class CommFlag
    {
        public CommFlag(CommFlag startFlag)
        {
            m_FCode = startFlag.m_FCode;
        }

        public CommFlag(int funCode)
        {
            m_FCode = (CommFunctionCode)funCode;
        }

        public CommFlag(CommFunctionCode funCode)
        {
            m_FCode = funCode;
        }
        private CommFunctionCode m_FCode = CommFunctionCode.Empty;

        public CommFunctionCode FCode
        {
            get { return m_FCode; }
        }
        //public static string Contact               { get { return "@"; } }/** 联络指令**/ //(@)
        //public static string ParameterSetting      { get { return "$"; } }/** 基本运行参数设置指令**/ //($)
        //public static string AccelerationSetting   { get { return "!"; } }/** 加速度参数设置指令**/ //(!)
        //public static string Run                   { get { return "#"; } }/** 运行指令**/ //(#)
        //public static string Stop                  { get { return "&"; } }/** 停止指令**/ //(&)
        //public static string ToZero                { get { return "%"; } }/** 归零指令**/ //(%)
        //public static string Query                 { get { return "?"; } }/** 查询当前位置指令**/ //(?)
        //public static string SingleEnd             { get { return ";"; } }/**                          **/ //(;)
        //public static string AllEnd                { get { return ">"; } }/**                          **/ //(;)
        //public static string Response              { get { return "="; } }/** 回答    **/ //(=)
        //public static string RelativeZero          { get { return "+"; } }/** 相对零位**/
        //public static string Separate              { get { return ","; } }/** 分隔符 **/ //（,）
        //public static string Space                 { get { return " "; } }
        //public static string Empty                 { get { return string.Empty; } }
        //public static string Command { get { return "CMD"; } }
        //public static string WrongFormat           { get { return "CMD CE"; } }

        public const string Contact = "@"; /** 联络指令**/ //(@)
        public const string ParameterSetting = "$"; /** 基本运行参数设置指令**/ //($)
        public const string AccelerationSetting = "!"; /** 加速度参数设置指令**/ //(!)
        public const string Run = "#"; /** 运行指令**/ //(#)
        public const string Stop = "&"; /** 停止指令**/ //(&)
        public const string ToZero = "%"; /** 归零指令**/ //(%)
        public const string Query = "?"; /** 查询当前位置指令**/ //(?)
        public const string SingleEnd = ";"; /**                          **/ //(;)
        public const string AllEnd = ">"; /**                          **/ //(;)
        public const string Response = "="; /** 回答    **/ //(=)
        public const string RelativeZero = "+"; /** 相对零位**/
        public const string StepperAngleFraction = "^";/**细分参数设置**/
        public const string ParameterPreserve = "<";/**参数保留**/
        public const string VZero = "-";/**虚拟零位**/
        public const string Separate = ","; /** 分隔符 **/ //（,）
        public const string Space = " "; 
        public const string Empty = ""; 

        public override string ToString()
        {
            switch (m_FCode)
            {
                case CommFunctionCode.Contact:              return "@";/** 联络指令                 **/ //(@)
                case CommFunctionCode.ParameterSetting:     return "$";/** 基本运行参数设置指令     **/ //($)
                case CommFunctionCode.AccelerationSetting:  return "!";/** 加速度参数设置指令       **/ //(!)
                case CommFunctionCode.Run:                  return "#";/** 运行指令                 **/ //(#)
                case CommFunctionCode.Stop:                 return "&";/** 停止指令                 **/ //(&)
                case CommFunctionCode.ToZero:               return "%";/** 归零指令                 **/ //(%)
                case CommFunctionCode.Query:                return "?";/** 查询当前位置指令         **/ //(?)
                case CommFunctionCode.SingleEnd:            return ";";/**                          **/ //(;)
                case CommFunctionCode.AllEnd:               return ">";/**                          **/ //(>)
                case CommFunctionCode.Response:             return "=";/** 回答                     **/ //(=)
                case CommFunctionCode.RelativeZero:         return "+";/** 相对零位**/
                case CommFunctionCode.StepperAngleFraction: return "^";/** 细分参数设置             **/ //(^)
                case CommFunctionCode.ParameterPreserve:    return "<";/**参数保留**/ //(<)
                case CommFunctionCode.VZero:                return "-";/**虚拟零位**/ //(<)
                case CommFunctionCode.Separate:             return ",";/** 分隔符 **/ //（,）
                case CommFunctionCode.Space:                return " ";
                case CommFunctionCode.Empty:                return string.Empty;
                //case CommFunctionCode.Command:              return "CMD";
                //case CommFunctionCode.WrongFormat:          return "CMD CE";
                default: return string.Empty;
            }
        }
    }

    public enum CommResultCode
    {
        Success = 0,
        NotOpen = -1,
        NoData = -2,
        NoVertify = -3,
        Failed = -4,
        WrongAxle = -5,
        WrongFrame = -6,
        Exceptions = -100,
        UnkownError = -200,
        WrongFormat = -300,

    }

    public class ErrorCode
    {
        public ErrorCode(ErrorCode eCode)
        {
            m_ECode = eCode.m_ECode;
        }

        public ErrorCode(int errorNo)
        {
            m_ECode = (CommResultCode)errorNo;
        }

        public ErrorCode(CommResultCode errorNo)
        {
            m_ECode = errorNo;
        }
        

        public CommResultCode CommErrorCode
        {
            get { return m_ECode; }
        }

        public int ErrorNo
        {
            get { return (int)m_ECode; }
        }

        private CommResultCode m_ECode = CommResultCode.UnkownError;
        public static int Success      { get { return (int)CommResultCode.Success;     } }
        public static int NotOpen      { get { return (int)CommResultCode.NotOpen;     } }
        public static int NoData       { get { return (int)CommResultCode.NoData;      } }
        public static int NoVertify    { get { return (int)CommResultCode.NoVertify;   } }
        public static int WrongAxle    { get { return (int)CommResultCode.WrongAxle;   } }
        public static int WrongFrame   { get { return (int)CommResultCode.WrongFrame; } }
        public static int WrongFormat  { get { return (int)CommResultCode.WrongFormat; } }
        public static int Exceptions   { get { return (int)CommResultCode.Exceptions;  } }
        public static int UnkownError  { get { return (int)CommResultCode.UnkownError; } }

        public override string ToString()
        {
            switch (CommErrorCode)
            {
                case CommResultCode.Success:          return "Success";
                case CommResultCode.NotOpen:          return "NotOpen";
                case CommResultCode.NoData:           return "NoData";
                case CommResultCode.NoVertify:        return "NoVertify";
                case CommResultCode.Exceptions:       return "Exceptions";
                case CommResultCode.WrongAxle:        return "WrongAxle";
                case CommResultCode.WrongFrame:       return "WrongFrame";
                case CommResultCode.WrongFormat:      return "WrongFormat";
                default:                              return "UnkownError";
            }
        }
    }
}
